/*
 * @(#)ParameterNode.java     14/08/02
 * 
 * Author             Rick Wilson
 * Copyright (c) 2014 Rick Wilson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of 'Taareekah' nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */



package parameters;

//~--- non-JDK imports --------------------------------------------------------

import org.w3c.dom.Document;
import org.w3c.dom.Element;

//~--- JDK imports ------------------------------------------------------------

import java.util.Collection;

/**
 * Base class for a node in the data tree.
 * @author rick
 */
public class ParameterNode {

    /**
     * The name of this node.
     */
    protected String	key;

    /**
     * This nodes parent node.
     */
    protected ParameterNode	parent;

    /**
     *
     */
    protected String	type;

    /**
     * Calculates this nodes depth by recursively 'climbing'. If this node has no parent its depth is 0.
     * @return The number of nodes between this node an the tree root, or 0 if this node is the root.
     */
    public int calculateDepth() {
	ParameterNode	current	= getParent();
	int		ret	= 0;

	while (current != null) {
	    ret++;
	    current	= current.getParent();
	}

	return ret;
    }

    /**
     * Returns true if this is a ParameterGroup.
     * @return false
     */
    public boolean isGroup() {
	return false;
    }

    /**
     * Returns true if this is a Parameter.
     * @return false
     */
    public boolean isParameter() {
	return false;
    }

    /**
     * Sets this nodes data to val. Overridden by Parameter.
     * @param <T> The type of data.
     * @param val The data.
     */
    public <T> void setValue(T val) {}

    /**
     * The type of this node.
     * @return The type.
     */
    public String getType() {
	return type;
    }

    /**
     * Returns this node as an XMLElement. Overridden in ParameterGroup and Parameter.
     * @param doc The document the element will belong to.
     * @return This node as an element.
     * @throws Exception Always because this function must be overridden.
     */
    public Element createXMLElement(Document doc) throws Exception {
	throw new Exception("Invalid Node");
    }

    /**
     * Returns this nodes value as an int. Overridden in Parameter.
     * @return This nodes value as an int.
     * @throws Exception Always because this function must be overridden.
     */
    public int getValueAsInt() throws Exception {
	throw new Exception("This is not a parameter");
    }

    /**
     * Returns this nodes value as an long. Overridden in Parameter.
     * @return This nodes value as an long.
     * @throws Exception Always because this function must be overridden.
     */
    public long getValueAsLong() throws Exception {
	throw new Exception("This is not a parameter");
    }

    /**
     * Returns this nodes value as an float. Overridden in Parameter.
     * @return This nodes value as an float.
     * @throws Exception Always because this function must be overridden.
     */
    public float getValueAsFloat() throws Exception {
	throw new Exception("This is not a parameter");
    }

    /**
     * Returns this nodes value as an boolean. Overridden in Parameter.
     * @return This nodes value as an boolean.
     * @throws Exception Always because this function must be overridden.
     */
    public boolean getValueAsBool() throws Exception {
	throw new Exception("This is not a parameter");
    }

    /**
     * Returns this nodes value as an double. Overridden in Parameter.
     * @return This nodes value as a double.
     * @throws Exception Always because this function must be overridden.
     */
    public double getValueAsDouble() throws Exception {
	throw new Exception("This is not a parameter");
    }

    /**
     * Returns this nodes value as a String. Overridden in Parameter.
     * @return This nodes value as a String.
     * @throws Exception Always because this function must be overridden.
     */
    public String getValueAsString() throws Exception {
	throw new Exception("This is not a parameter");
    }

    /**
     * Get the child node with name 'key'.
     * @param key The name of the child node.
     * @return The child node with name 'key'.
     * @throws Exception Always because this function must be overridden
     */
    public ParameterNode getChild(String key) throws Exception {
	throw new Exception("This is not a parametergroup");
    }

    /**
     * Returns a collection of all child nodes.
     * @return Null because this must be overridden.
     */
    public Collection<ParameterNode> getChildren() {
	return null;
    }

    /**
     * The number of child nodes.
     * @return -1 because this must be overridden
     */
    public int getNumChildren() {
	return -1;
    }

    /**
     *
     * @return
     */
    public boolean isSet() {
	return false;
    }

    /**
     * Returns the key that identifies this node.
     * @return This nodes key.
     */
    public String getKey() {
	return key;
    }

    /**
     * Sets new_parent as this nodes new parent and returns the old parent.
     * @param new_parent The node that will serve as this nodes new parent.
     * @return This nodes old parent node.
     */
    public ParameterNode setParent(ParameterNode new_parent) {
	ParameterNode	old_parent	= parent;

	parent	= new_parent;

	return old_parent;
    }

    /**
     * Returns this nodes parent.
     * @return The parent node.
     */
    public ParameterNode getParent() {
	return parent;
    }

    /**
     * Returns a fully qualified name in the form "rootkey.child0key. ... .thiskey".
     * Recursively climbs the parent nodes.
     * @return A fully qualified name for this node.
     */
    public String getFullName() {
	if (parent != null) {
	    return parent.getFullName() + "." + key;
	}

	return key;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
